package com.suge.home.module.screen.json;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.suge.common.query.order.SmsOrderQueryDO;
import com.suge.order.dao.dataobject.SmsOrderDO;
import com.suge.order.manager.SmsOrderManager;

public class QuerySendRecord {

	@Autowired
    private HttpServletRequest request;
	private static final Logger logger = LoggerFactory.getLogger(QuerySendRecord.class);
	@Autowired
	private SmsOrderManager smsOrderManager;
	@SuppressWarnings("unchecked")
	public void execute(Context context, Navigator nav) {
		String userId = request.getParameter("userId");
		if(userId==null || userId==""){
			context.put("isSuccess", false);
			return;
		}
		long userIdNum =  Long.parseLong(userId);
		String fromTime = request.getParameter("fromTime");
		String endTime = request.getParameter("endTime");
		String receiver = request.getParameter("receiver");
		String status = request.getParameter("status");
		String serviceId = request.getParameter("serviceId");
		
		String fmt = "yyyy-MM-dd";
		  
		SimpleDateFormat sdf = new SimpleDateFormat(fmt);
		SmsOrderQueryDO queryDO = new SmsOrderQueryDO();
		try {
			if(fromTime!=null && fromTime!=""){
				Date beginTimeDate = sdf.parse(fromTime);
				queryDO.setFromDate(beginTimeDate);
			}
			if(endTime!=null && endTime!=""){
				Date endTimeDate = sdf.parse(endTime);
				queryDO.setToDate(endTimeDate);
			}

			queryDO.setUserId(userIdNum);
			if(status==null || status==""){
				queryDO.setStatus(-5);
				queryDO.setStatus2(2);
			}
			else if("0".equals(status)){
				queryDO.setStatus(0);
				queryDO.setStatus2(2);
			}
			else if("1".equals(status)){
				queryDO.setStatus(-5);
				queryDO.setStatus2(0);
			}
			if(serviceId!=null && serviceId!="" && !"0".equals(serviceId)){
				queryDO.setOrderType(Integer.parseInt(serviceId)); 
			}

			if(receiver!=null && receiver!=""){
				queryDO.setReceiverTelephone(receiver);
			}
			 List<SmsOrderDO> listSmsDO = new ArrayList<SmsOrderDO>();
			 
			 listSmsDO =  smsOrderManager.getSmsOrderByQuery(queryDO);
			 logger.error("取出来的消息记录大小："+listSmsDO.size());
			 context.put("listSmsDO", listSmsDO);
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setSmsOrderManager(SmsOrderManager smsOrderManager) {
		this.smsOrderManager = smsOrderManager;
	}

	public SmsOrderManager getSmsOrderManager() {
		return smsOrderManager;
	}

}
